#include <cmath>
#include "EntPacman.h"

#define PI (double)3.141592654

EntPacman::EntPacman() {
	mouthangle = 0;
	mouthdirection = 5;
}

void EntPacman::think() {
	// game logic goes here for an Ent
	// can access the map with
	// handler->


	// animate
        mouthangle+=mouthdirection;

        if (mouthangle > 45)
                mouthdirection = -5;
        if (mouthangle <= 0)
                mouthdirection = 5;

}

void EntPacman::draw() {
	// display logic goes here
	// draw the object at 0,0,0
	// x,y are the gameboard, z is 0 for the level
	// remember Z is up now

        glBegin(GL_TRIANGLES);

        const int anglestep = 5;

        for (int phi = -90; phi < 90; phi += anglestep) {
                double sinphi = std::sin(phi * PI / 180.0);
                double cosphi = std::cos(phi * PI / 180.0);
                double sinphi2 = std::sin((phi + anglestep) * PI / 180.0);
                double cosphi2 = std::cos((phi + anglestep) * PI / 180.0);


                double sinthetam = std::sin(mouthangle * PI / 180.0);
                double costhetam = std::cos(mouthangle * PI / 180.0);

                //vect M1 = vect(sinphi, sinthetam * cosphi, costhetam * cosphi) * 8;
                //vect M2 = vect(sinphi2, sinthetam * cosphi2, costhetam * cosphi2) * 8;
                vect M1 = vect(sinphi, costhetam * cosphi, -sinthetam * cosphi) * 8;
                vect M2 = vect(sinphi2, costhetam * cosphi2, -sinthetam * cosphi2) * 8;

                vect normal = M2.cross(M1).normalized();
                glNormal3dv(normal);
                glColor3dv(normal * 0.5 + vect(0.5));
                glVertex3dv(vect::ZERO);
                glVertex3dv(M2);
                glVertex3dv(M1);
                M1 *= vect(1,1,-1);
                M2 *= vect(1,1,-1);
                normal = M1.cross(M2).normalized();
                glNormal3dv(normal);
                glColor3dv(normal * 0.5 + vect(0.5));
                glVertex3dv(vect::ZERO);
                glVertex3dv(M1);
                glVertex3dv(M2);

                for (int theta = mouthangle; theta < 360 - mouthangle; theta += anglestep) {
                        double sintheta = std::sin(theta * PI / 180.0);
                        double costheta = std::cos(theta * PI / 180.0);
                        double sintheta2 = std::sin((theta + anglestep) * PI / 180.0);
                        double costheta2 = std::cos((theta + anglestep) * PI / 180.0);

                        //vect A = vect(sinphi  , sintheta  * cosphi , costheta  * cosphi ) * 8;
                        //vect B = vect(sinphi  , sintheta2 * cosphi , costheta2 * cosphi ) * 8;
                        //vect C = vect(sinphi2 , sintheta  * cosphi2, costheta  * cosphi2) * 8;
                        //vect D = vect(sinphi2 , sintheta2 * cosphi2, costheta2 * cosphi2) * 8;
                        vect A = vect(sinphi  , costheta  * cosphi , -sintheta  * cosphi ) * 8;
                        vect B = vect(sinphi  , costheta2 * cosphi , -sintheta2 * cosphi ) * 8;
                        vect C = vect(sinphi2 , costheta  * cosphi2, -sintheta  * cosphi2) * 8;
                        vect D = vect(sinphi2 , costheta2 * cosphi2, -sintheta2 * cosphi2) * 8;

                        normal = (D - A).cross(B - A).normalized();
                        glNormal3dv(normal);
                        glColor3dv(normal * 0.5 + vect(0.5));
                        glVertex3dv(A);
                        glVertex3dv(D);
                        glVertex3dv(B);

                        normal = (C - A).cross(D - A).normalized();
                        glNormal3dv(normal);
                        glColor3dv(normal * 0.5 + vect(0.5));
                        glVertex3dv(A);
                        glVertex3dv(C);
                        glVertex3dv(D);
                }
        }

        glEnd();

}
